package com.cdshangpai.photoapp.ui.caseui;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.cdshangpai.photoapp.databinding.VideoListLayoutBinding;
import com.cdshangpai.photoapp.ui.OnListViewClickListener;
import com.cdshangpai.photoapp.ui.ViewMarginAdjust;
import com.cdshangpai.utils.db.VideoDb;
import com.cdshangpai.utils.db.beans.Video;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.List;

public class VideoFragment extends Fragment {
    VideoListLayoutBinding binding;
    VideoDb videoDb;
    private OnListViewClickListener onListViewClickListener=new OnListViewClickListener() {
        @Override
        public void onClick(Integer position) {
            Video video=binding.getAdapter().datas.get(position);

            String param= JSONObject.toJSONString(video);
            Intent intent=new Intent(getActivity(),VideoPlayActivity.class);
            intent.putExtra("param",param);
            getActivity().startActivity(intent);
        }
    };


    public VideoFragment()
    {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding= VideoListLayoutBinding.inflate(inflater);
        binding.videoList.setOverScrollMode(View.OVER_SCROLL_NEVER);
        binding.setView(this);

        initData();
        View view=binding.getRoot();
        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        ViewMarginAdjust.adjustListPosition(binding.videoList);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden)
        {
            search();
        }
    }

    /**
     * 初始化视频数据
     */
    private void initData() {
        FlexboxLayoutManager layoutManager=getFlexboxLayoutManager(false);
        binding.videoList.setLayoutManager(layoutManager);

        VideoListAdapter videoListAdapter=new VideoListAdapter(getActivity().getApplicationContext(),onListViewClickListener);
        binding.setAdapter(videoListAdapter);
        search();
    }

    private FlexboxLayoutManager getFlexboxLayoutManager(Boolean isCenter)
    {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this.getContext());
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        if(isCenter)
        {
            layoutManager.setJustifyContent(JustifyContent.CENTER);
            layoutManager.setAlignItems(AlignItems.CENTER);
        }
        else {
            layoutManager.setAlignItems(AlignItems.STRETCH);
            layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        }
        return layoutManager;
    }

    public void search()
    {
        if(videoDb==null)
        {
            videoDb=new VideoDb();
        }
        List<Video> videos=videoDb.query();
        binding.getAdapter().datas=videos;
        binding.getAdapter().notifyDataSetChanged();
    }
}
